from bs4 import BeautifulSoup
import requests

url = 'http://wh.58.com/bijibendiannao/32477917742667x.shtml'

def get_links_from(who_sells=0):
    urls = []
    list_view = 'http://xf.58.com/pbdn/{}/pn2/'.format(str(who_sells))
    wb_data = requests.get(list_view)
    soup = BeautifulSoup(wb_data.text, 'lxml')
    for link in soup.select('td.t a.t'):
        urls.append(link.get('href').split('?')[0])
    return urls

def get_views_from(url):
    id = url.split('/')[-1].strip('x.shtml')
    print(id)
    api = 'http://jst1.58.com/counter?infoid={}'.format(id)
    # 这个是找到了58的查询接口，不了解接口可以参照一下新浪微博接口的介绍
    js = requests.get(api)
    views = js.text.split('=')[-1]
    #return views
    print(views)

def get_item_info(url):

    wb_data = requests.get(url)
    soup = BeautifulSoup(wb_data.text, 'lxml')

    data = {
        'title': soup.title.text,
        'price': soup.select('.price')[0].text,
        'area' : soup.select('.crb_i')[0].text if soup.find_all('span', 'crb_i') else None,
        'date' : soup.select('.time')[0].text,
        'cate' : None
        # 'views':get_views_from(url)
    }
    print(data)

#get_item_info(url)
#get_links_from(0)
get_views_from(url)